package com.chengh.vhr.controller.system.basic;

import com.chengh.vhr.model.Menu;
import com.chengh.vhr.model.RespBean;
import com.chengh.vhr.model.Role;
import com.chengh.vhr.service.MenuRoleService;
import com.chengh.vhr.service.MenuService;
import com.chengh.vhr.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 权限组相关接口
 */
@RestController
@RequestMapping("/system/basic/permiss")
public class PerMissController {

    @Autowired
    RoleService roleService;

    @Autowired
    MenuService menuService;

    @Autowired
    MenuRoleService menuRoleService;

    /**
     * 返回所有的角色
     * @return
     */
    @GetMapping("/")
    public List<Role> getAllRole() {
        return roleService.getAllRole();
    }

    /**
     *  返回所有的菜单以及子节点菜单
     * @return
     */
    @GetMapping("/getMenusWithChildren")
    public List<Menu> getAllMenus() {
        return menuService.getMenusWithChildren();
    }

    /**
     * 根据角色id查询角色的拥有菜单id集合
     * @param rid 权限id
     * @return
     */
    @GetMapping("/mids/{rid}")
    public List<Integer> getMidsByRid(@PathVariable Integer rid) {
        return menuService.getMidsByRid(rid);
    }

    /**
     * 根据角色的id修改角色拥有的菜单,先删除后添加事务操作
     * @param par->rid 角色id
     * @param par-mIds> 菜单id集合
     * @return
     */
    @PutMapping("/")
    public RespBean updateMenuRole(@RequestBody Map<String,Object> par) {
        Integer rid = (Integer) par.get("rid");
        List<Integer> mIds = (List<Integer>) par.get("mIds");
        if(menuRoleService.updateMenuRole(rid,mIds)) {
            return RespBean.ok("更新成功!");
        }
        return RespBean.error("更新失败!");
    }

    /**
     * 添加角色
     * @param role 角色对象
     * @return
     */
    @PostMapping("/role")
    public RespBean addRole(@RequestBody Role role) {
        if(roleService.addRole(role) == 1) {
            return RespBean.ok("添加成功!");
        }
        return RespBean.error("添加失败!");

    }
    /**
     * 删除角色
     */
    @DeleteMapping("/role/{rid}")
    public RespBean deleteRoleByRid(@PathVariable Integer rid) {
        if(roleService.deleteByRid(rid) == 1){
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败!");
    }
}
